<?php defined('SYSPATH') OR die('No direct access allowed.');

class Confirm_Paid_Controller extends Master_Admin_Controller 
{
		
	public function index()
	{
		$this->page(1);
	}
	
	public function page($pagenum=1)
    {
		$view = new View('admin/confirm_paid');

		$this->set_title('ผู้ดูแลระบบ - อัลบัม');
    	
        $confirm_paids = new Albums_Model;

        // Instantiate Pagination, passing it the total number of product rows.
        $paging = new Pagination(array
		(
			'base_url'    => 'admin/confirm_paid/ajax_page/',
			'uri_segment' => 4,
			'total_items' => $confirm_paids->count_confirm_paids(),
		));

        // Render the page links
        $view->pagination = $paging->render();

        // Display X items per page, from offset = page number
        $view->confirm_paids = $confirm_paids->browse($paging->items_per_page, $paging->sql_offset);

    	$this->template->content = $view;
		$this->template->render(TRUE);
        
    }

    public function ajax_page($pagenum=1)
    {
    	if(request::is_ajax())
	    {
	    	$this->auto_render=false;
	    	$ajax = new ISS_Ajax();
	    	
	    	$view = new View('admin/confirm_paid');
	    	$confirm_paids = new Albums_Model;

	        // Instantiate Pagination, passing it the total number of product rows.
	        $paging = new Pagination(array
			(
				'base_url'    => 'admin/confirm_paid/ajax_page/',
				'uri_segment' => 4,
				'total_items' => $confirm_paids->count_confirm_paids(),
			));
	
	        // Render the page links
	        $view->pagination = $paging->render();
	
	        // Display X items per page, from offset = page number
	        $view->confirm_paids = $confirm_paids->browse($paging->items_per_page, $paging->sql_offset);
	    	$selector = "mainCont";
		    $content = $view;
			$ajax->addViewUpdate($selector, $content);
			echo json_encode($ajax->getHtmlAjax());
	    }
	    else {
	    	$this->page($pagenum);
	    }
    }
    
    public function add($status='') {
    	$view = new View('admin/confirm_paid_form');

		$this->set_title('ผู้ดูแลระบบ - สร้างอัลบัม');

		$form = array (
				'id' => '',
				'title'    => '',
				'cover_picture' => '',
				'released_date' => '',
		);

		$view->mode = 'add';
		$view->status = $status;
		if ($status != '') {
        	$form['title'] = (isset($_POST['txtTitle']) ? $_POST['txtTitle'] : '');
        	$form['cover_picture'] = (isset($_POST['txtCoverPicture']) ? $_POST['txtCoverPicture'] : '');
        	$form['released_date'] = (isset($_POST['txtReleasedDate']) ? $_POST['txtReleasedDate'] : '');
		}			  		  
		$view->form = $form;
		$view->errors = array();
    	$this->template->content = $view;
		$this->template->render(TRUE);
    }

    public function edit($id, $status='') {
    	$view = new View('admin/confirm_paid_form');

		$this->set_title('ผู้ดูแลระบบ - แก้ไขอัลบัม');
		$confirm_paids = new Albums_Model;
		
		$view->mode = 'edit';
		$view->status = $status;
		if ($status != '') {
        	$form['id'] = (isset($_POST['id']) ? $_POST['id'] : '');
			$form['title'] = (isset($_POST['txtTitle']) ? $_POST['txtTitle'] : '');
        	$form['cover_picture'] = $confirm_paids->get_image_fileneme($id);
        	$form['released_date'] = (isset($_POST['txtReleasedDate']) ? $_POST['txtReleasedDate'] : '');
        	$view->form = $form;
		}
		else {			  		  
			
			$forms = $confirm_paids->load($id);
			
			foreach ($forms as $row)
			{
				$view->form = $row; 	 
			}
		}
		$view->errors = array();
    	$this->template->content = $view;
		$this->template->render(TRUE);
    }
    
    public function del($id) {
    	if(request::is_ajax())
	    {
	    	$this->auto_render=false;
	    	$ajax = new ISS_Ajax();
	    	
	    	$confirm_paids = new Albums_Model;
	    	if ($confirm_paids->delete($id)) {
	    		$this->ajax_page(1);
	    	}
	    	else {
		    	$tag = "alert";
			    $script = "alert('". Kohana::lang('confirm_paid_error.save_result.delete_failed') ."')";
				$ajax->addExecJScript($tag, $script);
				echo json_encode($ajax->getHtmlAjax());
	    	}
	    }
	    else {
	    	$this->page(1);
	    }
    	
    }
    
    public function save() {
    	if($_POST) {
    		$post = new Validation($_POST);
			$post->pre_filter('trim', TRUE);
            $post->add_rules('txtTitle','required', 'length[2,255]');
            $post->add_rules('txtReleasedDate','required','valid::date');
        	$post->post_filter('ucfirst', 'name');

            $form = array (
        		'id' => '',
				'title' => '',
				'cover_picture' => '',
				'released_date' => '',
            );
        
        	if (!$post->validate())
	        {
	        	$errors = $post->errors('confirm_paid_error');
	        	$view = new View('admin/confirm_paid_form');
		
	        	$view->errors = $errors;
	        	$form['title'] = (isset($_POST['txtTitle']) ? $_POST['txtTitle'] : '');
	        	$form['cover_picture'] = (isset($_POST['txtCoverPicture']) ? $_POST['txtCoverPicture'] : '');
	        	$form['released_date'] = (isset($_POST['txtReleasedDate']) ? $_POST['txtReleasedDate'] : '');
	        	$view->form = $form;
				$this->set_title('ผู้ดูแลระบบ - สร้างอัลบัม');
				$view->mode = 'add';
				$this->template->content = $view;
				$this->template->render(TRUE);
	        }
	        else {
				$confirm_paids = new Albums_Model;
        		$confirm_paids->file_validate('txtCoverPicture');
        		$filename = $confirm_paids->get_filename();
        		if ($_POST['mode']=='edit') { 
        			$form['id'] = $_POST['id'];
        		} 
				$form['title'] = $_POST['txtTitle'];
				$form['released_date'] = $_POST['txtReleasedDate'];
        		if (!empty($filename)) {
		        	$form['cover_picture'] = str_replace(DOCROOT, '', $confirm_paids->get_filename());
        		}
        		
	        	if ($confirm_paids->save($form, $_POST['mode'])) {
	        		url::redirect('admin/confirm_paid');
	        	}
	        	else {
	        		$status = Kohana::lang('confirm_paid_error.save_result.save_failed');
	        		if ($_POST['mode']=='add') {
	        			$this->add($status);
	        		}
	        		else {
	        			$this->edit($_POST['id'], $status);
	        		}
	        	}
	        }
    		
    	}
    }

}
